package ass.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ass.beans.UserBean;
import ass.dao.UserDAO;
import ass.dao.support.UserDAOImpl;

/**
 * Servlet implementation class addTicketServlet
 */
public class BuyTicketServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuyTicketServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute("username");
		
		String title = (String) request.getParameter("title");
		String cinema = (String) request.getParameter("cinema");
		String time = (String) request.getParameter("time");
		String amount = (String) request.getParameter("amount");
		
		
		System.out.println(username+" "+title + " " + cinema + " " + time + " " + amount);
		
		
		
		
		
		UserDAO userDAO = new UserDAOImpl();
		
		UserBean userBean = userDAO.getUser(username);
		
		userBean.setTicket_title(title);
		userBean.setTicket_cinema(cinema);
		userBean.setTicket_show_time(time);
		userBean.setTicket_amount(Integer.parseInt(amount));
		userDAO.update(username, userBean);
		
		response.setContentType("text/html");// from response, set content type
		PrintWriter out = response.getWriter();// from response, get output

		out.write("Ticket info updated succesful");
		//out.close();
		
		String nextJSP = "";
		nextJSP = "/userSearch.jsp";
		request.setAttribute("movie_time_inserted",  "<font color='green'>Movie time inserted</font>");
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
		
//		request.setAttribute("updated", "<font color='green'> Ticket info updated </font>");
//		String nextJSP = "";
//		nextJSP = "/userTicketBooking.jsp";
//		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
//		dispatcher.forward(request, response);
	}

}
